package com.scs.application.modules.dept.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scs.application.modules.base.entity.Warehouse;
import com.scs.application.modules.dept.dto.ReturnReagentDTO;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.dept.service.UseReagentService;
import com.scs.application.modules.dept.entity.UseReagent;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试剂三级库使用明细 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/dept/use_reagent")
public class UseReagentController extends CrudController<UseReagentService, UseReagent>{

    /**
     * 扫码试剂构建消耗明细
     */
    @PostMapping("/getBySn")
    public RestResult getBySn(@RequestBody Map<String, String> params) {
        UseReagent use = JSONObject.toJavaObject(JSONObject.parseObject(params.get("form")), UseReagent.class);
        return RestResult.ok("查询成功", service.getBySn(params.get("stockId"), use));
    }

    /**
     * 扫码试剂构建消耗明细
     */
    @PostMapping("/getBySns")
    public RestResult getBySns(@RequestBody Map<String, String> params) {
        UseReagent use = JSONObject.toJavaObject(JSONObject.parseObject(params.get("form")), UseReagent.class);
        List<String> stockIds = JSONArray.toJavaObject(JSONArray.parseArray(params.get("stockIds")), List.class);
        return RestResult.ok("查询成功", service.getBySns(stockIds, use));
    }

    /**
     * 添加/更新数据 试剂消耗
     */
    @PostMapping("/saveItems")
    public RestResult saveItems(@RequestBody Map<String, String> params) {
        List<String> stockIds = JSONArray.toJavaObject(JSONArray.parseArray(params.get("stockIds")), List.class);
        List<UseReagent> items = JSONArray.parseArray(params.get("items")).toJavaList(UseReagent.class);
        service.saveItems(items, stockIds);
        return RestResult.ok("数据保存成功");
    }

    /**
     * 试剂调拨
     */
    @PostMapping("/transfer")
    public RestResult transfer(@RequestBody Map<String, Object> params) {
        List<String> stockIds = (List<String>) params.get("stockIds");
        List<UseReagent> items = ((JSONArray) params.get("items")).toJavaList(UseReagent.class);
        ReturnReagentDTO transferDTO = ((JSONObject) params.get("transfer")).toJavaObject(ReturnReagentDTO.class);
        Warehouse warehouse = ((JSONObject) params.get("warehouse")).toJavaObject(Warehouse.class);

        service.transfer(items, stockIds, transferDTO, warehouse);
        return RestResult.ok("试剂调拨成功");
    }

}
